package com.reveregroup.friendconnect;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.Widget;

/**
 * 
 * <p>
 * Reference: <a
 * href="http://code.google.com/apis/friendconnect/gfc_buttons.html"
 * >http://code.google.com/apis/friendconnect/gfc_buttons.html</a>
 * 
 * @author David Wolverton
 */
public class LoginButton extends Widget {
	private String text;
	private Style style;
	private boolean rendered;
	
	public LoginButton() {
		setElement(DOM.createDiv());
	}
	
	public LoginButton(String text, Style style) {
		setElement(DOM.createDiv());
		this.text = text;
		this.style = style;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		if (Util.eq(this.text, text))
			return;
		this.text = text;
		if (rendered)
			render();
	}

	public Style getStyle() {
		return style;
	}

	public void setStyle(Style style) {
		if (Util.eq(this.style, style))
			return;
		this.style = style;
		if (rendered)
			render();
	}
	
	@Override
	protected void onAttach() {
		if (!rendered)
			render();
		super.onAttach();
	}

	public void render() {
		if (rendered)
			getElement().setInnerHTML("");
		renderSignInButton(getElement(), text, style);
		rendered = true;
	}
	
	public static void renderSignInButton(Element element, String text, Style style) {
		renderSignInButton(element, text, style == null ? null : style.getString());
	}

	private static native void renderSignInButton(Element element, String text, String style) /*-{
		var params = {id: element};
		if (text)
			params.text = text;
		if (style)
			params.style = style;
		
		$wnd.google.friendconnect.renderSignInButton(params);
	}-*/;
	
	public static enum Style {
		STANDARD, LONG, TEXT;
		
		private String getString() {
			return name().toLowerCase();
		}
	}
}
